$(function () {

    var setting = {
        view: {
            showLine: true,

        },
        async: {
            enable: true,
            url: "/dept/getDeptData",//url地址
            type: 'post',
            autoParam: ["id"],
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pid",
                rootPId: 0
            }
        },
        callback: {
            onClick: zTreeOnClick,
            onAsyncError: zTreeOnAsyncError,
            onAsyncSuccess: function (event, treeId, treeNode, msg) {
            }
        }
    };

    function zTreeOnClick(event, treeId, treeNode) {
        $('#inputDeptId').attr("value", treeNode.id)

    }

    function zTreeOnAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
        alert("加载错误：" + XMLHttpRequest);
    }

    $(document).ready(function () {
        $.fn.zTree.init($("#tree"), setting);
    });
})

//提交
function createBtn() {
    var inputName = '';
    if ($('#inputName').val() == '' || $('#inputName').val() == null) {
        alert("课程名不能为空")
        return
    } else {
        inputName = $('#inputName').val();
    }
    var inputRoom = '';
    if ($('#inputRoom').val() == '' || $('#inputRoom').val() == null) {
        alert("教室选项未填（如若未定填写：待定）")
        return
    } else {
        inputRoom = $('#inputRoom').val();
    }
    var inputCount = '';
    if ($('#inputCount').val() == '' || $('#inputCount').val() == null) {
        alert("开选人数不能为空")
        return
    } else {
        inputCount = $('#inputCount').val();
    }
    var inputCredit = '';
    if ($('#inputCredit').val() == '' || $('#inputCredit').val() == null) {
        alert("课程学分未填")
        return
    } else {
        inputCredit = $('#inputCredit').val();
    }
    var inputDeptId = ''
    if ($('#inputDeptId').val() == '' || $('#inputDeptId').val() == null) {
        alert("请选择课程开放于哪个学院 或 面向全校")
        return
    } else {
        inputDeptId = $('#inputDeptId').val();
    }

    var remark = $('#remark').val();
    var inputStart = '';
    var inputEnd = '';
    var inputWeek = '';
    var inputSLesson = '';
    var inputElesson = '';
    var timetable = ''
    if ($('#inputStart option:selected').val() == '' || $('#inputEnd option:selected').val() == '' || $('#inputWeek1 option:selected').val() == '' || $('#inputSLesson1 option:selected').val() == '' || $('#inputElesson1 option:selected').val() == '') {
        alert("请确保开课时间选项至少一行完整");
        return
    } else {
        inputStart = $('#inputStart').val();
        inputEnd = $('#inputEnd').val();
        inputWeek = $('#inputWeek1').val();
        inputSLesson = $('#inputSLesson1').val();
        inputElesson = $('#inputElesson1').val();
        timetable = inputStart + '|' + inputEnd + '|' + inputWeek + '|' + inputSLesson + '|' + inputElesson;
        if ($('#inputWeek2 option:selected').val() == '' || $('#inputSLesson2 option:selected').val() == '' || $('#inputElesson2 option:selected').val() == '') {

        } else {
            inputWeek = $('#inputWeek2').val();
            inputSLesson = $('#inputSLesson2').val();
            inputElesson = $('#inputElesson2').val();
            timetable += '|' + inputWeek + '|' + inputSLesson + '|' + inputElesson;
            if ($('#inputWeek3 option:selected').val() == '' || $('#inputSLesson3 option:selected').val() == '' || $('#inputElesson3 option:selected').val() == '') {

            } else {
                inputWeek = $('#inputWeek3').val();
                inputSLesson = $('#inputSLesson3').val();
                inputElesson = $('#inputElesson3').val();
                timetable += '|' + inputWeek + '|' + inputSLesson + '|' + inputElesson;
            }
        }
        //发送给后台
        $.post(
            "/course/create",
            {
                courseName: inputName,
                classroom: inputRoom,
                count: inputCount,
                credit: inputCredit,
                deptId: inputDeptId,
                timetable: timetable,
                remarks: remark
            },
            function (response) {
                alert(response.msg)
                if (response.code==0){
                    window.location.href="/course/history";
                }
            }
        )
    }

}

function editBtn() {
    var inputName = '';
    if ($('#inputName').val() == '' || $('#inputName').val() == null) {
        alert("课程名不能为空")
        return
    } else {
        inputName = $('#inputName').val();
    }
    var inputRoom = '';
    if ($('#inputRoom').val() == '' || $('#inputRoom').val() == null) {
        alert("教室选项未填（如若未定填写：待定）")
        return
    } else {
        inputRoom = $('#inputRoom').val();
    }
    var inputCount = '';
    if ($('#inputCount').val() == '' || $('#inputCount').val() == null) {
        alert("开选人数不能为空")
        return
    } else {
        inputCount = $('#inputCount').val();
    }
    var inputCredit = '';
    if ($('#inputCredit').val() == '' || $('#inputCredit').val() == null) {
        alert("课程学分未填")
        return
    } else {
        inputCredit = $('#inputCredit').val();
    }
    var inputDeptId = ''
    if ($('#inputDeptId').val() == '' || $('#inputDeptId').val() == null) {
        alert("请选择课程开放于哪个学院 或 面向全校")
        return
    } else {
        inputDeptId = $('#inputDeptId').val();
    }

    var remark = $('#remark').val();
    var inputStart = '';
    var inputEnd = '';
    var inputWeek = '';
    var inputSLesson = '';
    var inputElesson = '';
    var timetable = ''
    if ($('#inputStart option:selected').val() == '' || $('#inputEnd option:selected').val() == '' || $('#inputWeek1 option:selected').val() == '' || $('#inputSLesson1 option:selected').val() == '' || $('#inputElesson1 option:selected').val() == '') {
        alert("请确保开课时间选项至少一行完整");
        return
    } else {
        inputStart = $('#inputStart').val();
        inputEnd = $('#inputEnd').val();
        inputWeek = $('#inputWeek1').val();
        inputSLesson = $('#inputSLesson1').val();
        inputElesson = $('#inputElesson1').val();
        timetable = inputStart + '|' + inputEnd + '|' + inputWeek + '|' + inputSLesson + '|' + inputElesson;
        if ($('#inputWeek2 option:selected').val() == '' || $('#inputSLesson2 option:selected').val() == '' || $('#inputElesson2 option:selected').val() == '') {

        } else {
            inputWeek = $('#inputWeek2').val();
            inputSLesson = $('#inputSLesson2').val();
            inputElesson = $('#inputElesson2').val();
            timetable += '|' + inputWeek + '|' + inputSLesson + '|' + inputElesson;
            if ($('#inputWeek3 option:selected').val() == '' || $('#inputSLesson3 option:selected').val() == '' || $('#inputElesson3 option:selected').val() == '') {

            } else {
                inputWeek = $('#inputWeek3').val();
                inputSLesson = $('#inputSLesson3').val();
                inputElesson = $('#inputElesson3').val();
                timetable += '|' + inputWeek + '|' + inputSLesson + '|' + inputElesson;
            }
        }
        var courseId = $('#courseIdInput').val();
        //发送给后台
        $.post(
            "/course/editCourse",
            {
                courseId: courseId,
                courseName: inputName,
                classroom: inputRoom,
                count: inputCount,
                credit: inputCredit,
                deptId: inputDeptId,
                timetable: timetable,
                remarks: remark
            },
            function (response) {
                alert(response.msg)
                if(response.code==0){
                    window.location.href="/course/history";
                }

            }
        )
    }
}